{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27a88f99",
   "metadata": {},
   "source": [
    "> 时间:2002-11-21/week14\n",
    "\n",
    "> 目标：函数与模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9935344a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello\"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ff9b92a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello李华~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "221d18f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    \"\"\"y=2x的函数计算\"\"\"\n",
    "    print(2*x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "64ec1b4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0d73c02b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "        print(\"https//www.nfu.edu.cn/\"+类别+\"/index\"+str(page)+\".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "70ed83c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https//www.nfu.edu.cn/xydt/index0.htm\n",
      "https//www.nfu.edu.cn/xydt/index1.htm\n",
      "https//www.nfu.edu.cn/xydt/index2.htm\n",
      "https//www.nfu.edu.cn/xydt/index3.htm\n",
      "https//www.nfu.edu.cn/xydt/index4.htm\n",
      "https//www.nfu.edu.cn/xydt/index5.htm\n",
      "https//www.nfu.edu.cn/xydt/index6.htm\n",
      "https//www.nfu.edu.cn/xydt/index7.htm\n",
      "https//www.nfu.edu.cn/xydt/index8.htm\n",
      "https//www.nfu.edu.cn/xydt/index9.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"xydt\",10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8241cc2a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2e749a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 本周目标\n",
    "> 1. 函数的定义\n",
    "> 2. 如何使用函数\n",
    "> 3. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fd71a27",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c0b4a44e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 如何使用函数（传递实参）1.positional argument（位置参数） 2. keyword argument(关键字参数)\n",
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d06590ae",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [22]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "24764b2f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4103ecfd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e7847d0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2214092b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y,z):\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60bab196",
   "metadata": {},
   "outputs": [],
   "source": [
    "f(3,2,5) #根据上面的经验，这个应该是位置参数吧（领悟"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c7805ac7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "f(y=10,x=3,z=7)# 这个应该是关键字参数（领悟），关键字参数考虑顺序么？（不考虑顺序）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "d5a3da77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "f(3,z=10,y=9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bccfa5a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "f(3,y=10,z=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "885f5d22",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (1857774082.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [30]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(3,y=9,10)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(3,y=9,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9e2296e1",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "f() got multiple values for argument 'y'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [31]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43my\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m11\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mTypeError\u001b[0m: f() got multiple values for argument 'y'"
     ]
    }
   ],
   "source": [
    "f(3,10,y=11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c3b75cf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "10\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "f(3,10,z=11)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad1e2a51",
   "metadata": {},
   "source": [
    "## 3.默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4df763bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a\"+ animal_type+\".\")\n",
    "    print(\"My\"+animal_type+\"'s name is\"+pet_name.title()+\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e31fa90e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f2111cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e5dd95a7",
   "metadata": {},
   "source": [
    "# 3. 函数的返回值\n",
    "> 几乎所有编程语言，函数是要求必须有返回值\n",
    "> python 作为高级语言，可以不写返回值，但是一般情况下，也是要写的，不写的话，默认返回none\n",
    "> 3. 函数返回值：return\n",
    ">  3.1.return 可以返回过程运算的结果\n",
    ">  3.2.return 可以返回过程自定义结果（由程序开发人员/设计人员定制的）\n",
    "\n",
    "# 4. 函数的优势：\n",
    "* 1. 可重复使用\n",
    "* 2. 具有强大的可拓展性\n",
    "* 3. 具有可定制化型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2e9854f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y=x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5f61b75e",
   "metadata": {},
   "outputs": [],
   "source": [
    "result=f(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a51bce61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42f4a71a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a8a5b4c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "raw",
   "id": "dec9066c",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
